#!/usr/bin/env python3
# 版本: 1.0.0
# 作者: 陈振玺
# 功能: Smart Email 使用示例

"""
Smart Email 使用示例

本文件展示了如何在Python代码中使用Smart Email的各种功能。
"""

# 所有包导入必须放在文件头部
from smart_email import ConfigManager, EmailSender, LLMClient, FileHandler
from pathlib import Path


def example_config_management():
    """配置管理示例"""
    print("=== 配置管理示例 ===")
    
    config_manager = ConfigManager()
    
    # 设置邮件配置
    email_config = {
        'smtp_server': 'smtp.gmail.com',
        'smtp_port': 587,
        'email': 'your_email@gmail.com',
        'password': 'your_app_password',
        'use_tls': True
    }
    config_manager.set_email_config(email_config)
    
    # 设置LLM配置
    llm_config = {
        'api_url': 'https://api.openai.com/v1/chat/completions',
        'api_key': 'your_openai_api_key',
        'model': 'gpt-3.5-turbo',
        'max_tokens': 1000,
        'temperature': 0.7
    }
    config_manager.set_llm_config(llm_config)
    
    print("配置已保存")


def example_email_sending():
    """邮件发送示例"""
    print("=== 邮件发送示例 ===")
    
    config_manager = ConfigManager()
    
    # 检查配置是否存在
    if not config_manager.is_email_configured():
        print("请先配置邮件设置")
        return
    
    email_config = config_manager.get_email_config()
    sender = EmailSender(email_config)
    
    # 发送简单邮件
    try:
        sender.send_email(
            to=['recipient@example.com'],
            subject='测试邮件',
            content='这是一封测试邮件。',
            from_name='Smart Email 用户'
        )
        print("邮件发送成功")
    except Exception as e:
        print(f"邮件发送失败: {e}")


def example_llm_optimization():
    """LLM措辞优化示例"""
    print("=== LLM措辞优化示例 ===")
    
    config_manager = ConfigManager()
    
    if not config_manager.is_llm_configured():
        print("请先配置LLM设置")
        return
    
    llm_config = config_manager.get_llm_config()
    llm_client = LLMClient(llm_config)
    
    # 优化邮件内容
    original_content = "我想请假明天不来上班。"
    
    try:
        optimized_content = llm_client.optimize_email_content(
            content=original_content,
            tone='professional'
        )
        print(f"原始内容: {original_content}")
        print(f"优化后内容: {optimized_content}")
    except Exception as e:
        print(f"内容优化失败: {e}")


def example_file_handling():
    """文件处理示例"""
    print("=== 文件处理示例 ===")
    
    file_handler = FileHandler()
    
    # 创建测试文件
    test_dir = Path('test_files')
    test_dir.mkdir(exist_ok=True)
    
    # 创建一些测试文件
    (test_dir / 'file1.txt').write_text('测试文件1内容')
    (test_dir / 'file2.txt').write_text('测试文件2内容')
    
    # 打包文件夹
    try:
        zip_path = file_handler.create_zip_archive(
            source_path=str(test_dir),
            output_path='test_files.zip'
        )
        print(f"文件夹已打包为: {zip_path}")
        
        # 获取文件大小
        file_size = file_handler.get_file_size(zip_path)
        print(f"压缩包大小: {file_size} 字节")
        
    except Exception as e:
        print(f"文件处理失败: {e}")
    finally:
        # 清理测试文件
        import shutil
        if test_dir.exists():
            shutil.rmtree(test_dir)
        if Path('test_files.zip').exists():
            Path('test_files.zip').unlink()


def example_complete_workflow():
    """完整工作流程示例"""
    print("=== 完整工作流程示例 ===")
    
    config_manager = ConfigManager()
    
    # 检查配置
    if not (config_manager.is_email_configured() and config_manager.is_llm_configured()):
        print("请先完成邮件和LLM配置")
        return
    
    # 获取配置
    email_config = config_manager.get_email_config()
    llm_config = config_manager.get_llm_config()
    
    # 初始化客户端
    sender = EmailSender(email_config)
    llm_client = LLMClient(llm_config)
    file_handler = FileHandler()
    
    # 准备邮件内容
    original_content = "请帮我审核一下附件中的文档，谢谢。"
    
    try:
        # 优化邮件内容
        optimized_content = llm_client.optimize_email_content(
            content=original_content,
            tone='professional'
        )
        
        # 创建测试附件
        test_file = Path('test_attachment.txt')
        test_file.write_text('这是一个测试附件')
        
        # 发送邮件
        sender.send_email(
            to=['recipient@example.com'],
            subject='文档审核请求',
            content=optimized_content,
            attachments=[str(test_file)],
            from_name='Smart Email 用户'
        )
        
        print("完整工作流程执行成功")
        
    except Exception as e:
        print(f"工作流程执行失败: {e}")
    finally:
        # 清理测试文件
        if test_file.exists():
            test_file.unlink()


if __name__ == '__main__':
    print("Smart Email 使用示例")
    print("注意：运行前请先配置邮件和LLM设置")
    print()
    
    # 运行示例（注释掉需要真实配置的部分）
    # example_config_management()
    # example_email_sending()
    # example_llm_optimization()
    example_file_handling()
    # example_complete_workflow()
    
    print("\n示例运行完成")